package com.lee.study.nettty.server;

import java.net.InetSocketAddress;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class TcpServer {

	@Autowired
	@Qualifier("serverBootstrap")
	private ServerBootstrap b;
	
	@Autowired
	@Qualifier("tcpSocketAddress")
	private InetSocketAddress tcpPort;
	
	private Channel serverChannel;
	
	@PostConstruct
	public void start() throws Exception{
		System.out.println("Starting server at " + tcpPort );
		serverChannel = b.bind(tcpPort).sync().channel().closeFuture().sync().channel();
	}
	
	@PreDestroy
	public void destroy(){
		serverChannel.close();
	}
}
